package com.zmn.oms.dubbo.impl.normal.order;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.business.interfaces.log.OrderLogBService;
import com.zmn.oms.dubbo.dto.common.work.OrderLogDIO;
import com.zmn.oms.dubbo.dto.common.work.OrderLogDRO;
import com.zmn.oms.dubbo.interfaces.normal.order.OrderLogListRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.log.OrderLogQuery;
import com.zmn.oms.model.entity.log.OrderLog;
import lombok.extern.log4j.Log4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author sunlife
 * @date:2021/6/25 4:15 下午
 * description:
 */
@Log4j
@Service(loadbalance = "roundrobin", timeout = 10000, retries = 0, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class OrderLogListRemoteServiceImpl implements OrderLogListRemoteService {

    @Autowired
    OrderLogBService orderLogBService;

    @Override
    public ResponseDTO<List<OrderLogDRO>> listOrderLogByQuery(OrderLogDIO query) {
        if (query == null) {
            return ResponseDTO.fail("入参不能为空");
        }

        OrderLogQuery orderLogQuery = BeanMapper.map(query, OrderLogQuery.class);
        List<OrderLog> orderLogs = orderLogBService.findOrderLogsByQuery(orderLogQuery);
        if (CollectionUtil.isNullOrEmpty(orderLogs)) {
            return ResponseDTO.fail("订单日志集合为空");
        }
        if (NumberUtil.isNotNullOrZero(query.getLogType())) {
            orderLogs.removeIf(orderLog -> {
                return !Objects.equals(orderLog.getType(), query.getLogType());
            });
        }
        return ResponseDTO.success(BeanMapper.mapList(orderLogs, OrderLogDRO.class));
    }
}
